package com.example.goodtrash.Fragment;

import android.app.ProgressDialog;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.goodtrash.R;
import com.example.goodtrash.model.NewsResult;
import com.example.goodtrash.model.TrashNews;
import com.example.goodtrash.util.HttpUtil;
import com.example.goodtrash.util.Utility;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;


public class NewsFragment extends Fragment {
    private RecyclerView recyclerView;
    private List<TrashNews> dataList=new ArrayList<>();
    private TrashNewsAdapter adapter;
    private ProgressDialog progressDialog;

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";


    private String mParam1;
    private String mParam2;

    public NewsFragment() {

    }


    public static NewsFragment newInstance(String param1, String param2) {
        NewsFragment fragment = new NewsFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_news, container, false);
        recyclerView=view.findViewById(R.id.rv_news);
        adapter=new TrashNewsAdapter(dataList,getContext(),getActivity());
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(adapter);
        showLoadingDialog();
        HttpUtil.sendNewsRequest(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                Toast.makeText(getContext(),"获取新闻失败",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
               String responseText=response.body().string();
               NewsResult newsResult = Utility.handleTrashNewsResponse(responseText);
               getActivity().runOnUiThread(new Runnable() {
                   @Override
                   public void run() {
                       if (newsResult!=null&&"success".equals(newsResult.getMsg())){
                           closeProgressDialog();
                           dataList.clear();
                           List<TrashNews> newsList=newsResult.getNewsList();
                           for (TrashNews trashNews:newsList){
                               dataList.add(trashNews);
                           }
                           adapter.notifyDataSetChanged();
                       }else {
                           closeProgressDialog();
                           Toast.makeText(getContext(),"解析失败",Toast.LENGTH_SHORT).show();
                       }
                   }
               });
            }
        });
        return view;
    }

    private void showLoadingDialog(){
        if(progressDialog==null){
            progressDialog=new ProgressDialog(getContext());
            progressDialog.setMessage("正在加载.....");
            progressDialog.setCanceledOnTouchOutside(false);
        }
        progressDialog.show();
    }

    private void closeProgressDialog(){
        if(progressDialog!=null){
            progressDialog.dismiss();
        }
    }
}